<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
  <head>
    <title>KRPC::Connection</title>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
    <link href='../../css/style.css' media='screen' rel='stylesheet' type='text/css'>
    <script type='text/javascript'>
      //<![CDATA[
        function popupCode(url) {
          window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
        }
        
        function toggleCode(id) {
          var code = document.getElementById(id)
        
          code.style.display = code.style.display != 'block' ? 'block' : 'none'
          return true
        }
        
        // Make codeblocks hidden by default
        document.writeln('<' + 'style type="text/css">.method .source pre { display: none }<\/style>')
      //]]>
    </script>
  </head>
  <body class='page'>
    <div class='class' id='wrapper'>
      <div class='header'>
        <h1 class='name'>
          <span class='type'>class</span>
          KRPC::Connection
        </h1>
        <ol class='paths'>
          <li>
            <a target="docwin" href="../../files/lib/krpc/connection_rb.html">lib/krpc/connection.rb</a>
          </li>
        </ol>
        <div class='parent'>
          Superclass:
          <strong><a target="docwin" href="../Object.html">Object</a></strong>
        </div>
      </div>
      <div id='content'>
        <div id='text'>
          <div id='description'>
            
            <p>A TCP <a href="Connection.html">Connection</a>.</p>
          </div>
          <div id='method-list'>
            <h2>Methods</h2>
            <h3>Public Class</h3>
            <ol>
              <li><a target="docwin" href="#method-c-new">new</a></li>
            </ol>
            <h3>Public Instance</h3>
            <ol>
              <li><a target="docwin" href="#method-i-cleanup">cleanup</a></li>
              <li><a target="docwin" href="#method-i-close">close</a></li>
              <li><a target="docwin" href="#method-i-connect">connect</a></li>
              <li><a target="docwin" href="#method-i-connected-3F">connected?</a></li>
              <li><a target="docwin" href="#method-i-handshake">handshake</a></li>
              <li><a target="docwin" href="#attribute-i-host">host</a></li>
              <li><a target="docwin" href="#attribute-i-port">port</a></li>
              <li><a target="docwin" href="#method-i-recv">recv</a></li>
              <li><a target="docwin" href="#method-i-recv_varint">recv_varint</a></li>
              <li><a target="docwin" href="#method-i-send">send</a></li>
              <li><a target="docwin" href="#attribute-i-socket">socket</a></li>
            </ol>
            <h3>Protected Instance</h3>
            <ol>
              <li><a target="docwin" href="#method-i-trim_fill">trim_fill</a></li>
            </ol>
          </div>
          <div id='context'>
          </div>
          <div id='section'>
            <div id='constants-list'>
              <h2>Constants</h2>
              <div class='name-list'>
                <table summary='Constants'>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>DEFAULT_SERVER_HOST</td>
                    <td>=</td>
                    <td class='context-item-value'>"127.0.0.1"</td>
                    <td>&nbsp;</td>
                    <td class='context-item-desc'></td>
                  </tr>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>DEFAULT_SERVER_RPC_PORT</td>
                    <td>=</td>
                    <td class='context-item-value'>50000</td>
                    <td>&nbsp;</td>
                    <td class='context-item-desc'></td>
                  </tr>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>DEFAULT_SERVER_STREAM_PORT</td>
                    <td>=</td>
                    <td class='context-item-value'>50001</td>
                    <td>&nbsp;</td>
                    <td class='context-item-desc'></td>
                  </tr>
                </table>
              </div>
            </div>
            <div id='attribute-list'>
              <h2 class='section-bar'>Attributes</h2>
              <div class='name-list'>
                <table>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>
                      <a name='attribute-i-host'>host</a>
                    </td>
                    <td class='context-item-value'>[R]</td>
                    <td class='context-item-desc'></td>
                  </tr>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>
                      <a name='attribute-i-port'>port</a>
                    </td>
                    <td class='context-item-value'>[R]</td>
                    <td class='context-item-desc'></td>
                  </tr>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>
                      <a name='attribute-i-socket'>socket</a>
                    </td>
                    <td class='context-item-value'>[R]</td>
                    <td class='context-item-desc'></td>
                  </tr>
                </table>
              </div>
            </div>
            <div id='methods'>
              <h2>Public Class methods</h2>
              <div class='method public-class' id='method-method-c-new'>
                <a name='method-c-new'></a>
                <div class='synopsis'>
                  <span class='name'>new</span>
                  <span class='arguments'>(host, port)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-new-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-new-source'><span class="ruby-comment"># File lib/krpc/connection.rb, line 16</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">host</span>, <span class="ruby-identifier">port</span>)&#x000A;  <span class="ruby-ivar">@host</span>, <span class="ruby-ivar">@port</span> = <span class="ruby-identifier">host</span>, <span class="ruby-identifier">port</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <h2>Public Instance methods</h2>
              <div class='method public-instance' id='method-method-i-cleanup'>
                <a name='method-i-cleanup'></a>
                <div class='synopsis'>
                  <span class='name'>cleanup</span>
                  <span class='arguments'>()</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-cleanup-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-cleanup-source'><span class="ruby-comment"># File lib/krpc/connection.rb, line 50</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">cleanup</span>; <span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-instance' id='method-method-i-close'>
                <a name='method-i-close'></a>
                <div class='synopsis'>
                  <span class='name'>close</span>
                  <span class='arguments'>()</span>
                </div>
                <div class='description'>
                  
                  <p>Close connection and clean up.</p>
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-close-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-close-source'><span class="ruby-comment"># File lib/krpc/connection.rb, line 36</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">close</span>&#x000A;  <span class="ruby-keyword">if</span> <span class="ruby-identifier">connected?</span>&#x000A;    <span class="ruby-identifier">socket</span>.<span class="ruby-identifier">close</span>&#x000A;    <span class="ruby-identifier">cleanup</span>&#x000A;    <span class="ruby-keyword">true</span>&#x000A;  <span class="ruby-keyword">else</span> <span class="ruby-keyword">false</span> <span class="ruby-keyword">end</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-instance' id='method-method-i-connect'>
                <a name='method-i-connect'></a>
                <div class='synopsis'>
                  <span class='name'>connect</span>
                  <span class='arguments'>()</span>
                </div>
                <div class='description'>
                  
                  <p>Connect and perform handshake.</p>
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-connect-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-connect-source'><span class="ruby-comment"># File lib/krpc/connection.rb, line 21</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">connect</span>&#x000A;  <span class="ruby-keyword">if</span> <span class="ruby-identifier">connected?</span> <span class="ruby-keyword">then</span> <span class="ruby-identifier">raise</span>(<span class="ruby-constant">ConnectionError</span>, <span class="ruby-string">&quot;Already connected&quot;</span>)&#x000A;  <span class="ruby-keyword">else</span> &#x000A;    <span class="ruby-ivar">@socket</span> = <span class="ruby-constant">TCPSocket</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">host</span>, <span class="ruby-identifier">port</span>) &#x000A;    <span class="ruby-keyword">begin</span>&#x000A;      <span class="ruby-identifier">handshake</span>&#x000A;    <span class="ruby-keyword">rescue</span> <span class="ruby-constant">Exception</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>&#x000A;      <span class="ruby-identifier">close</span>&#x000A;      <span class="ruby-identifier">raise</span> <span class="ruby-identifier">e</span>&#x000A;    <span class="ruby-keyword">end</span>&#x000A;  <span class="ruby-keyword">end</span>&#x000A;  <span class="ruby-keyword">self</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-instance' id='method-method-i-connected-3F'>
                <a name='method-i-connected-3F'></a>
                <div class='synopsis'>
                  <span class='name'>connected?</span>
                  <span class='arguments'>()</span>
                </div>
                <div class='description'>
                  
                  <p>Return <code>true</code> if connected to a server, <code>false</code>
                  otherwise.</p>
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-connected-3F-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-connected-3F-source'><span class="ruby-comment"># File lib/krpc/connection.rb, line 45</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">connected?</span>&#x000A;  <span class="ruby-operator">!</span><span class="ruby-identifier">socket</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-operator">!</span><span class="ruby-identifier">socket</span>.<span class="ruby-identifier">closed?</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-instance' id='method-method-i-handshake'>
                <a name='method-i-handshake'></a>
                <div class='synopsis'>
                  <span class='name'>handshake</span>
                  <span class='arguments'>()</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-handshake-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-handshake-source'><span class="ruby-comment"># File lib/krpc/connection.rb, line 49</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">handshake</span>; <span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-instance' id='method-method-i-recv'>
                <a name='method-i-recv'></a>
                <div class='synopsis'>
                  <span class='name'>recv</span>
                  <span class='arguments'>(maxlen = 1)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-recv-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-recv-source'><span class="ruby-comment"># File lib/krpc/connection.rb, line 53</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">recv</span>(<span class="ruby-identifier">maxlen</span> = <span class="ruby-value">1</span>) <span class="ruby-ivar">@socket</span>.<span class="ruby-identifier">recv</span>(<span class="ruby-identifier">maxlen</span>) <span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-instance' id='method-method-i-recv_varint'>
                <a name='method-i-recv_varint'></a>
                <div class='synopsis'>
                  <span class='name'>recv_varint</span>
                  <span class='arguments'>()</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-recv_varint-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-recv_varint-source'><span class="ruby-comment"># File lib/krpc/connection.rb, line 55</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">recv_varint</span>&#x000A;  <span class="ruby-identifier">int_val</span> = <span class="ruby-value">0</span>&#x000A;  <span class="ruby-identifier">shift</span> = <span class="ruby-value">0</span>&#x000A;  <span class="ruby-identifier">loop</span> <span class="ruby-keyword">do</span>&#x000A;    <span class="ruby-identifier">byte</span> = <span class="ruby-identifier">recv</span>.<span class="ruby-identifier">ord</span>&#x000A;    <span class="ruby-identifier">int_val</span> <span class="ruby-operator">|=</span> (<span class="ruby-identifier">byte</span> <span class="ruby-operator">&amp;</span> <span class="ruby-value">0b0111_1111</span>) <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">shift</span>&#x000A;    <span class="ruby-keyword">return</span> <span class="ruby-identifier">int_val</span> <span class="ruby-keyword">if</span> (<span class="ruby-identifier">byte</span> <span class="ruby-operator">&amp;</span> <span class="ruby-value">0b1000_0000</span>) <span class="ruby-operator">==</span> <span class="ruby-value">0</span>&#x000A;    <span class="ruby-identifier">shift</span> <span class="ruby-operator">+=</span> <span class="ruby-value">7</span>&#x000A;    <span class="ruby-identifier">raise</span>(<span class="ruby-constant">RuntimeError</span>, <span class="ruby-string">&quot;too many bytes when decoding varint&quot;</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">shift</span> <span class="ruby-operator">&gt;=</span> <span class="ruby-value">64</span>&#x000A;  <span class="ruby-keyword">end</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-instance' id='method-method-i-send'>
                <a name='method-i-send'></a>
                <div class='synopsis'>
                  <span class='name'>send</span>
                  <span class='arguments'>(msg)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-send-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-send-source'><span class="ruby-comment"># File lib/krpc/connection.rb, line 52</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">send</span>(<span class="ruby-identifier">msg</span>) <span class="ruby-ivar">@socket</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">msg</span>, <span class="ruby-value">0</span>) <span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <h2>Protected Instance methods</h2>
              <div class='method protected-instance' id='method-method-i-trim_fill'>
                <a name='method-i-trim_fill'></a>
                <div class='synopsis'>
                  <span class='name'>trim_fill</span>
                  <span class='arguments'>(str, len, fill_char = "\x00")</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-trim_fill-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-trim_fill-source'><span class="ruby-comment"># File lib/krpc/connection.rb, line 69</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">trim_fill</span>(<span class="ruby-identifier">str</span>, <span class="ruby-identifier">len</span>, <span class="ruby-identifier">fill_char</span> = <span class="ruby-string">&quot;\x00&quot;</span>)&#x000A;  <span class="ruby-identifier">str</span> = <span class="ruby-identifier">str</span>.<span class="ruby-identifier">encode</span>(<span class="ruby-string">&quot;UTF-8&quot;</span>)[<span class="ruby-value">0</span>, <span class="ruby-identifier">len</span>]&#x000A;  <span class="ruby-identifier">str</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">fill_char</span><span class="ruby-operator">*</span>(<span class="ruby-identifier">len</span><span class="ruby-operator">-</span><span class="ruby-identifier">str</span>.<span class="ruby-identifier">length</span>)&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div id='footer-push'></div>
    </div>
    <div id='footer'>
      <a target="docwin" href="http://github.com/mislav/hanna/tree/master"><strong>Hanna</strong> RDoc template</a>
    </div>
  </body>
</html>
